package com.xoa.plugin;

import com.xoa.dao.budget.BudgetItemMapper;
import com.xoa.dao.budget.BudgetProcessLogMapper;
import com.xoa.dao.budget.BudgetingProcessMapper;
import com.xoa.dao.flowhook.FlowHookMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.global.proxy.TriggerSubject;
import com.xoa.model.budget.BudgetItem;
import com.xoa.model.budget.BudgetProcessLog;
import com.xoa.model.budget.BudgetingProcess;
import com.xoa.model.budget.BudgetingProcessFlowRunPrcs;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.model.workflow.FlowProcess;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.util.budgetUtil.budgeUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.xoa.util.budgetUtil.budgeUtil.insertBudgetProcess;
import static com.xoa.util.budgetUtil.budgeUtil.queryTriggerField;

@Service
public class BudgetingProcessTrigger implements TriggerSubject {

    @Resource
    private BudgetingProcessMapper budgetingProcessMapper;
    @Resource
    private BudgetItemMapper budgetItemMapper;
    @Resource
    private FlowProcessMapper flowProcessMapper;
    @Autowired
    private FlowHookMapper flowHookMapper;
    @Autowired
    BudgetProcessLogMapper budgetProcessLogMapper;
    @Autowired
    UsersMapper usersMapper;

    private static SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    @Override
    public void doRun(String company, Object... ags) {

        ContextHolder.setConsumerType(company);
        TriggerPlugIn tri = (TriggerPlugIn) ags[0];
        BudgetingProcess budgetingProcess = new BudgetingProcess();

        FlowProcess flowProcess = new FlowProcess();
        if(tri.getFlowPrcs().indexOf(",") != -1){
            String[] strings = tri.getFlowPrcs().split(",");
            flowProcess = flowProcessMapper.findPrcessLine(Integer.parseInt(tri.getFlowId()), Integer.parseInt(strings[0]));
        }else {
            flowProcess = flowProcessMapper.findPrcessLine(Integer.parseInt(tri.getFlowId()), Integer.parseInt(tri.getFlowPrcs()));
        }


        if (flowProcess != null) {
            String prcs = flowProcess.getPrcsTo();
            if (!"".equals(prcs) && prcs != null) {
                prcs = prcs.substring(0, prcs.length() - 1);
                if ("0".equals(prcs)) {
                    budgetingProcess.setAllow("1");


                } else {

                    budgetingProcess.setAllow("0");

                }
            }
        }
        FlowHookWithBLOBs flowHookNew = new FlowHookWithBLOBs();
        flowHookNew.setHid(tri.getHookId());
        FlowHookWithBLOBs flowHook = flowHookMapper.selectByhid(flowHookNew);
        Map<Object, Object> flowHookMaps = null;
        if (flowHook != null) {
            flowHookMaps = queryTriggerField(tri.getModify(), tri.getReject(), flowHook.getMap(), flowHook.getDataDirection());
        }
        BudgetingProcess process = null;
        if (flowHookMaps != null) {
            process = insertBudgetProcess(budgetingProcess, flowHookMaps);
            //避免pc端deptId为空
            if(!StringUtils.checkNull(budgetingProcess.getApplicant())){
                Users user = usersMapper.getUserId(budgetingProcess.getApplicant());
                budgetingProcess.setDeptId(user.getDeptId().toString());
            }


            Map<Integer,String> map =new HashMap<Integer,String>();
            if (!StringUtils.checkNull(budgetingProcess.getCashierOpinion()) && !"``````0``|`````````".equals(budgetingProcess.getCashierOpinion())&&!"|```````0`".equals(budgetingProcess.getCashierOpinion())&&!"|`````````".equals(budgetingProcess.getCashierOpinion())) {
                String cashierOpinion = budgetingProcess.getCashierOpinion();
                cashierOpinion = cashierOpinion.substring(0, cashierOpinion.indexOf("|"));
                String[] split = cashierOpinion.replace("\r\n", "*").split("\\*");
                for (int i = 0, length = split.length; i < length; i++) {
                    if (!StringUtils.checkNull(split[i])) {
                        String[] split1 = split[i].split("`");
                        int length1 = split1.length;
                        map.put(Integer.valueOf(split1[length1-1]),split1[length1-2]);
                    }
                }
            }else if (!StringUtils.checkNull(budgetingProcess.getBreakUpPlan()) && !"``````0``|`````````".equals(budgetingProcess.getBreakUpPlan())&&!"|```````0`".equals(budgetingProcess.getBreakUpPlan())&&!"|`````````".equals(budgetingProcess.getBreakUpPlan())) {
                String breakUpPlan = budgetingProcess.getBreakUpPlan();
                breakUpPlan = breakUpPlan.substring(0, breakUpPlan.indexOf("|"));
                String[] split = breakUpPlan.replace("\r\n", "*").split("\\*");
                for (int i = 0, length = split.length; i < length; i++) {
                    if (!StringUtils.checkNull(split[i])) {
                        String[] split1 = split[i].split("`");
                        int length1 = split1.length;
                        map.put(Integer.valueOf(split1[length1-1]),split1[length1-2]);
                    }
                }
            } else if (budgetingProcess.getBudgetId()!=null) {
                map.put(budgetingProcess.getBudgetId(), String.valueOf(budgetingProcess.getPayment()));
            }
            if (tri.getRunId() != null) {
                // 根据runId查询 如果存在代表不是新建 如果不存在代表是新建的流程
                List<BudgetingProcessFlowRunPrcs> budgetingProcessFlowRunPrcs = budgetingProcessMapper.selBudgetProcessByRunId(Integer.valueOf(tri.getRunId()));
                if (budgetingProcessFlowRunPrcs != null && budgetingProcessFlowRunPrcs.size() > 0) {
                    if (Integer.parseInt(tri.getFlowPrcs()) == 1) {
                        if(Integer.parseInt(tri.getFlowPrcs()) < Integer.parseInt(tri.getPrcsId())){
                            for(BudgetingProcessFlowRunPrcs budgetingProcessFlowRunPrcs1 : budgetingProcessFlowRunPrcs){
                                if(budgetingProcessFlowRunPrcs1.getAdvance().compareTo(BigDecimal.ZERO) != 0){
                                    BudgetingProcess budgetingProcess1 = new BudgetingProcess();
                                    budgetingProcess1.setAdvance(BigDecimal.ZERO);
                                    budgetingProcess1.setBudgetProcessId(budgetingProcessFlowRunPrcs1.getBudgetProcessId());
                                    budgetingProcessMapper.updateByPrimaryKeySelective(budgetingProcess1);
                                }else {
                                    BudgetingProcess budgetingProcess1 = new BudgetingProcess();
                                    budgetingProcess1.setActualExpenditure(BigDecimal.ZERO);
                                    budgetingProcess1.setAmountWords("");
                                    budgetingProcess1.setBudgetProcessId(budgetingProcessFlowRunPrcs1.getBudgetProcessId());
                                    budgetingProcessMapper.updateByPrimaryKeySelective(budgetingProcess1);
                                    BudgetItem budgetItem1 = budgetItemMapper.selectByPrimaryKey(budgetingProcessFlowRunPrcs1.getBudgetId());
                                    BudgetItem budgetItem = new BudgetItem();
                                    budgetItem.setRemainingAmount(budgetItem1.getRemainingAmount().add(budgetingProcessFlowRunPrcs1.getActualExpenditure()));
                                    budgetItem.setBudgetId(budgetingProcessFlowRunPrcs1.getBudgetId());
                                    budgetItemMapper.updateByPrimaryKeySelective(budgetItem);
                                }
                            }
                        }
//                        for (Map.Entry<Integer,String> entry: map.entrySet()){
//
//                            BudgetItem budgetItem = budgetItemMapper.selBudgetItemById(entry.getKey());
//
//                            budgetingProcess.setRunId(Integer.valueOf(tri.getRunId()));
//                            budgetingProcess.setBudgetId(entry.getKey());
//                            budgetingProcess.setAdvance(budgeUtil.getTwoDecimal(entry.getValue()));
//                            budgetingProcess.setActualExpenditure(BigDecimal.ZERO);
//                            budgetingProcess.setBudgetItemName(budgetItem.getBudgetItemName());
//
//                            budgetingProcessMapper.update(budgetingProcess);
//                        }
                        //System.out.println("1111");
                    }else {
                        for (int i = 0, size = budgetingProcessFlowRunPrcs.size(); i < size; i++) {
                            // 台账信息
                            BudgetingProcessFlowRunPrcs budgetingProcessFlowRunPrcs1 = budgetingProcessFlowRunPrcs.get(i);
                            // 获取项目信息
                            BudgetItem budgetItem = budgetItemMapper.selBudgetItemById(budgetingProcessFlowRunPrcs1.getBudgetId());
                            // 判断是否是最终确认支出余额
                            //System.out.println(budgetingProcess.getActualExpenditure());
                            if (!StringUtils.checkNull(budgetingProcess.getActualExpenditure() + "") && !(budgetingProcess.getActualExpenditure().compareTo(BigDecimal.ZERO) == 0)&&budgetingProcessFlowRunPrcs1.getAdvance().compareTo(BigDecimal.ZERO)>0) {
                                //bigdecimal类型比较是否为0不能使用equals，equals首先比较的是小数点位数，
                                // 也就是说0.0和0.00用equals方法比较返回的是false，
                                // 可以使用 budgetingProcess.getActualExpenditure().compareTo(BigDecimal.ZERO) == 0
                                // 更新项目余额
                                BigDecimal integer = budgetItem.getRemainingAmount();
                                BigDecimal integer1 = budgeUtil.getTwoDecimal(map.get(budgetingProcessFlowRunPrcs1.getBudgetId()));
                                budgetItem.setRemainingAmount(integer.subtract(integer1));
                                // 设置支出金额
                                budgetingProcess.setActualExpenditure(integer1);
                                // 恢复预支出为0
                                budgetingProcess.setAdvance(BigDecimal.ZERO);
                                budgetingProcess.setAllow("1");
                                // 更新
                                budgetItemMapper.updateByPrimaryKeySelective(budgetItem);
                            } else {
                                budgetingProcess.setActualExpenditure(BigDecimal.ZERO);
                                if (!StringUtils.checkNull(map.get(budgetItem.getBudgetId()))) {
                                    budgetingProcess.setAdvance(budgeUtil.getTwoDecimal(map.get(budgetItem.getBudgetId())));
                                }
                                // 该判断用来判断流程是否已经结束
                                if(budgetingProcessFlowRunPrcs1.getAdvance().compareTo(BigDecimal.ZERO)==0&&budgetingProcessFlowRunPrcs1.getActualExpenditure().compareTo(BigDecimal.ZERO)>0){
                                    budgetingProcess.setActualExpenditure(budgetingProcessFlowRunPrcs1.getActualExpenditure());
                                    budgetingProcess.setAdvance(budgetingProcessFlowRunPrcs1.getAdvance());
                                }
                            }

                            Integer prcs = budgetingProcessMapper.selBudgetFlowPrcs(Integer.valueOf(tri.getRunId()));
                            if (prcs == 8) {
                                budgetingProcess.setAdvance(BigDecimal.ZERO);
                            }

                            budgetingProcess.setBudgetProcessId(budgetingProcessFlowRunPrcs1.getBudgetProcessId());
                            budgetingProcess.setBudgetId(budgetingProcessFlowRunPrcs1.getBudgetId());
                            budgetingProcess.setBudgetItemName(budgetingProcessFlowRunPrcs1.getBudgetItemName());
                            int a = budgetingProcessMapper.updateByPrimaryKeySelective(budgetingProcess);
                            map.remove(budgetItem.getBudgetId());
                        }
                        // 判断是否是拆分了项目支出
                        if (map != null && map.size() > 0) {
                            for (Map.Entry<Integer, String> entry : map.entrySet()) {

                                BudgetItem budgetItem = budgetItemMapper.selBudgetItemById(entry.getKey());

                                // 判断是否是最终确认支出余额
                                if (!StringUtils.checkNull(budgetingProcess.getActualExpenditure() + "") && !(budgetingProcess.getActualExpenditure().compareTo(BigDecimal.ZERO) == 0)) {
                                    // 更新项目余额
                                    BigDecimal integer = budgetItem.getRemainingAmount();
                                    BigDecimal integer1 = budgeUtil.getTwoDecimal(map.get(entry.getKey()));
                                    budgetItem.setRemainingAmount(integer.subtract(integer1));
                                    // 设置支出金额
                                    budgetingProcess.setActualExpenditure(integer1);
                                    // 恢复预支出为0
                                    budgetingProcess.setAdvance(BigDecimal.ZERO);
                                    budgetingProcess.setAllow("1");
                                    // 更新
                                    budgetItemMapper.updateByPrimaryKeySelective(budgetItem);
                                } else {
                                    budgetingProcess.setActualExpenditure(BigDecimal.ZERO);
                                    budgetingProcess.setAdvance(budgeUtil.getTwoDecimal(map.get(budgetItem.getBudgetId())));
                                }

                                budgetingProcess.setRunId(Integer.valueOf(tri.getRunId()));
                                budgetingProcess.setBudgetId(entry.getKey());
                                budgetingProcess.setBudgetItemName(budgetItem.getBudgetItemName());
                                budgetingProcess.setBudgetProcessId(null);
                                budgetingProcessMapper.insertSelective(budgetingProcess);
                            }

                        }
                    }
                } else {
                    if (map!=null&&map.size()>0) {
                        for (Map.Entry<Integer,String> entry: map.entrySet()){

                            BudgetItem budgetItem = budgetItemMapper.selBudgetItemById(entry.getKey());

                            budgetingProcess.setRunId(Integer.valueOf(tri.getRunId()));
                            budgetingProcess.setBudgetId(entry.getKey());
                            budgetingProcess.setAdvance(budgeUtil.getTwoDecimal(entry.getValue()));
                            budgetingProcess.setActualExpenditure(BigDecimal.ZERO);
                            budgetingProcess.setBudgetItemName(budgetItem.getBudgetItemName());

                            budgetingProcessMapper.insertSelective(budgetingProcess);
                        }

                    }
                }

            }
            List<BudgetProcessLog> list = new ArrayList<>();
            BudgetProcessLog budgetProcessLog = null;
            // 该判断是判断是否是后面的步骤 退回到申请人步骤
            if("1".equals(tri.getFlowPrcs())){
                budgetProcessLogBackTrue1(tri,budgetProcessLog,budgetingProcess,list);
            } else {
                // 把flowPrcs替换成当前的实际步骤id
                switch (Integer.valueOf(tri.getCurrentPrcsId())) {
                    case 1:
                        this.budgetProcessLogTrue1(tri,budgetProcessLog,budgetingProcess,list);
                        break;
                    case 4:
                        this.budgetProcessLogTrue4(tri,budgetProcessLog,process,list);
                        break;
                    case 7:
                        this.budgetProcessLogTrue7(tri,budgetProcessLog,budgetingProcess,list);
                        break;
                    case 8:
                        this.budgetProcessLogTrue8(tri,budgetProcessLog,budgetingProcess,list);
                        break;
                    default:
                        this.budgetProcessLogTrue(tri,budgetProcessLog,budgetingProcess,list);
                        break;
                }
            }

            //将数据插入日志表
//            int flag = 0;
//            for (BudgetProcessLog processLog : list) {
//                List<Integer> integers = budgetProcessLogMapper.queryLogByRunIdAndPricsId(processLog);
//                if(integers != null && integers.size() > 0){
//                    processLog.setLogId(integers.get(flag));
//                    budgetProcessLogMapper.updateByPrimaryKey(processLog);
//                }else {
//                    budgetProcessLogMapper.insertSelective(processLog);
//                }
//                flag++ ;
//            }
            //将数据插入日志表

            ///-----======= 注意，这里不要删除，用于存储日志方面的逻辑，为防止客户再次更改日志存储方式
            /*List<Integer> integers = new ArrayList<>();
            if(list!= null && list.size()>0) {
                integers = budgetProcessLogMapper.queryLogByRunIdAndPricsId(list.get(0));
            }
            if(integers != null && integers.size() > 0){
                int flag = 0;
                for(BudgetProcessLog budgetProcessLog1 : list){
                    budgetProcessLog1.setLogId(integers.get(flag));
                    budgetProcessLogMapper.updateByPrimaryKey(budgetProcessLog1);
                    flag++ ;
                }
            }else {
                for (BudgetProcessLog budgetProcessLog1 : list) {
                    budgetProcessLogMapper.insertSelective(budgetProcessLog1);
                }
            }*/

            for (BudgetProcessLog budgetProcessLog1 : list) {
                budgetProcessLogMapper.insertSelective(budgetProcessLog1);
            }

        }

    }

//    public Map<Object, Object> queryTriggerField(String modifydata, String fromdate, String maps, Integer num) {
//
//        //获取json串 进行判断
//        //获取业务引擎接口的数据进行分开，组合map
//        /**
//         * 拿自己内置的数据去写的
//         */
//
//        String[] a = maps.split(",");
//        Map<String, Object> map = new HashMap<String, Object>();
//        for (int i = 0; i < a.length; i++) {
//            String i1 = a[i];
//            String i2[] = i1.split("=>");
//            String i3 = i2[0];
//            String i4 = i2[1];
//            map.put(i3, i4);
//            //key           USER_ID
//            //value         申请人
//
//        }
//        JSONArray json = new JSONArray();
//        //String fromdateOne="["+fromdate+"]";
//        Map<String, Object> from = json.parseObject(
//                fromdate, new TypeReference<Map<String, Object>>() {
//                });
//
//
//        List<Map<String, Object>> modify = json.parseObject(modifydata, List.class);
//        //List<Map<String, Object>> from = json.parseObject(fromdate, List.class);
//        Map<Object, Object> analysis = new HashMap<Object, Object>();
//        Map<Object, Object> returnMap = new HashMap<Object, Object>();
//        for (Map<String, Object> mapmodify : modify) {
//            for (Map.Entry<String, Object> entry1 : from.entrySet()) {
//                if (mapmodify.get("key").equals(entry1.getValue())) {
//                    analysis.put(entry1.getKey(), mapmodify.get("value"));
//                }
//
//            }                        // 申请人   value   USER_ID
//        }
//
//        if ("1".equals(num)) {
//            for (Map.Entry<Object, Object> entry : analysis.entrySet()) {//key 申请人 value  1
//                for (Map.Entry<String, Object> entry1 : map.entrySet()) {
//                    if (entry.getKey().equals(entry1.getValue())) {
//                        returnMap.put(entry1.getKey(), entry.getValue());
//
//                    }
//
//                }
//
//            }
//        } else if ("2".equals(num)) {
//
//        }
//        for (Map.Entry<Object, Object> entry : analysis.entrySet()) {//key 申请人 value  1
//            for (Map.Entry<String, Object> entry1 : map.entrySet()) {
//                if (entry.getKey().equals(entry1.getKey())) {
//                    returnMap.put(entry1.getValue(), entry.getValue());
//
//                }
//
//            }
//
//
//        }
//
//
//        return returnMap;
//    }



    private void setBudgetProcessLog(BudgetProcessLog budgetProcessLog, Integer budgetId, String money, Integer runId, Integer prcsId, Integer uid, String logTime, Integer logType) {
        budgetProcessLog.setBudgetId(budgetId);
        budgetProcessLog.setMoney(money);
        budgetProcessLog.setRunId(runId);
        budgetProcessLog.setPrcsId(prcsId);
        budgetProcessLog.setUid(uid);
        budgetProcessLog.setLogTime(logTime);
        budgetProcessLog.setLogType(logType);
    }


    //其他不涉及金额的步骤日志记录
    private void budgetProcessLogTrue(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
//        budgetProcessLog = new BudgetProcessLog();
//        budgetProcessLog.setMoney("0");
//        budgetProcessLog.setLogType(0);
//        budgetProcessLog.setBudgetId(budgetingProcess.getBudgetId());
//        budgetProcessLog.setLogTime(sdf.format(new Date()));
//        budgetProcessLog.setPrcsId(Integer.valueOf(tri.getFlowPrcs()));
//        budgetProcessLog.setRunId(Integer.valueOf(tri.getRunId()));
//        budgetProcessLog.setUid(tri.getUsers().getUid());
//        list.add(budgetProcessLog);
//        budgetProcessLog = null;
//
        if (!StringUtils.checkNull(budgetingProcess.getCashierOpinion()) && !"``````0``|`````````".equals(budgetingProcess.getCashierOpinion())&&!"|```````0`".equals(budgetingProcess.getCashierOpinion())&&!"|`````````".equals(budgetingProcess.getCashierOpinion())) {
            getCashierOpinion(tri,budgetProcessLog,budgetingProcess,list);
        }else if (!StringUtils.checkNull(budgetingProcess.getBreakUpPlan()) && !"``````0``|`````````".equals(budgetingProcess.getBreakUpPlan()) && !"|```````0`".equals(budgetingProcess.getBreakUpPlan()) && !"|`````````".equals(budgetingProcess.getBreakUpPlan())) {
            getBreakUpPlan(tri,budgetProcessLog,budgetingProcess,list);
        }else {
            budgetProcessLog = new BudgetProcessLog();
            budgetProcessLog.setMoney("0");
            budgetProcessLog.setLogType(0);
            budgetProcessLog.setBudgetId(budgetingProcess.getBudgetId());
            budgetProcessLog.setLogTime(sdf.format(new Date()));
            budgetProcessLog.setPrcsId(Integer.valueOf(tri.getPrcsId()));
            budgetProcessLog.setRunId(Integer.valueOf(tri.getRunId()));
            budgetProcessLog.setUid(tri.getUsers().getUid());
            list.add(budgetProcessLog);
            budgetProcessLog = null;
        }
    }


    //申请人步骤日志记录
    private void budgetProcessLogTrue1(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
        budgetProcessLog = new BudgetProcessLog();
        budgetProcessLog.setMoney("-"+budgetingProcess.getPayment());
        budgetProcessLog.setLogType(2);
        budgetProcessLog.setBudgetId(budgetingProcess.getBudgetId());
        budgetProcessLog.setLogTime(sdf.format(new Date()));
        budgetProcessLog.setPrcsId(Integer.valueOf(tri.getPrcsId()));
        budgetProcessLog.setRunId(Integer.valueOf(tri.getRunId()));
        budgetProcessLog.setUid(tri.getUsers().getUid());
        list.add(budgetProcessLog);
        budgetProcessLog = null;
    }

    //退回到申请人步骤日志记录
    private void budgetProcessLogBackTrue1(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
        budgetProcessLog = new BudgetProcessLog();
        budgetProcessLog.setMoney("+"+budgetingProcess.getPayment());
        budgetProcessLog.setLogType(1);
        budgetProcessLog.setBudgetId(budgetingProcess.getBudgetId());
        budgetProcessLog.setLogTime(sdf.format(new Date()));
        budgetProcessLog.setPrcsId(Integer.valueOf(tri.getPrcsId()));
        budgetProcessLog.setRunId(Integer.valueOf(tri.getRunId()));
        budgetProcessLog.setUid(tri.getUsers().getUid());
        list.add(budgetProcessLog);
        budgetProcessLog = null;
    }

    //会计步骤日志记录
    private void budgetProcessLogTrue4(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list) {

        //会计处加回申请人的本次支出金额
        budgetProcessLog = new BudgetProcessLog();
        setBudgetProcessLog(budgetProcessLog,
                budgetingProcess.getBudgetId(),
                "+" + budgetingProcess.getPayment(),
                Integer.valueOf(tri.getRunId()),
                Integer.valueOf(tri.getPrcsId()),
                tri.getUsers().getUid(),
                sdf.format(new Date()), 1);
        list.add(budgetProcessLog);
        budgetProcessLog = null;

        if (!StringUtils.checkNull(budgetingProcess.getBreakUpPlan()) && !"``````0``|`````````".equals(budgetingProcess.getBreakUpPlan()) && !"|```````0`".equals(budgetingProcess.getBreakUpPlan()) && !"|`````````".equals(budgetingProcess.getBreakUpPlan())) {
            getBreakUpPlan(tri,budgetProcessLog,budgetingProcess,list);
        }

    }



    //出纳步骤日志记录
    private void budgetProcessLogTrue7(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list) {
//        //会计处加回申请人的本次支出金额
//        budgetProcessLog = new BudgetProcessLog();
//        setBudgetProcessLog(budgetProcessLog,
//                budgetingProcess.getBudgetId(),
//                "+" + budgetingProcess.getPayment(),
//                Integer.valueOf(tri.getRunId()),
//                Integer.valueOf(tri.getFlowPrcs()),
//                tri.getUsers().getUid(),
//                sdf.format(new Date()), 1);
//        list.add(budgetProcessLog);
//        budgetProcessLog = null;

        String breakUpPlan = budgetingProcess.getBreakUpPlan();
        breakUpPlan = breakUpPlan.substring(0, breakUpPlan.indexOf("|"));
        String[] split = breakUpPlan.replace("\r\n", "*").split("\\*");

        for (int i = 0, length = split.length; i < length; i++) {
            if (!StringUtils.checkNull(split[i])) {
                String[] split1 = split[i].split("`");

                budgetProcessLog = new BudgetProcessLog();
                this.setBudgetProcessLog(budgetProcessLog,
                        Integer.valueOf(split1[8]),
                        "+" + split1[7],
                        Integer.valueOf(tri.getRunId()),
                        Integer.valueOf(tri.getPrcsId()),
                        tri.getUsers().getUid(),
                        sdf.format(new Date()), 1);
                list.add(budgetProcessLog);
                budgetProcessLog = null;

            }
        }

        if (!StringUtils.checkNull(budgetingProcess.getCashierOpinion()) && !"``````0``|`````````".equals(budgetingProcess.getCashierOpinion())&&!"|```````0`".equals(budgetingProcess.getCashierOpinion())&&!"|`````````".equals(budgetingProcess.getCashierOpinion())) {
            getCashierOpinion(tri,budgetProcessLog,budgetingProcess,list);
        }
    }

    private void getBreakUpPlan(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
        String breakUpPlan = budgetingProcess.getBreakUpPlan();
        breakUpPlan = breakUpPlan.substring(0, breakUpPlan.indexOf("|"));
        String[] split = breakUpPlan.replace("\r\n", "*").split("\\*");

        for (int i = 0, length = split.length; i < length; i++) {
            if (!StringUtils.checkNull(split[i])) {
                String[] split1 = split[i].split("`");

                budgetProcessLog = new BudgetProcessLog();
                this.setBudgetProcessLog(budgetProcessLog,
                        Integer.valueOf(split1[8]),
                        "4".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?("".equals(split1[7])?"0":"-"+split1[7]):"0",
                        Integer.valueOf(tri.getRunId()),
                        Integer.valueOf(tri.getPrcsId()),
                        tri.getUsers().getUid(),
                        sdf.format(new Date()), "4".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?2:0);
                list.add(budgetProcessLog);
                budgetProcessLog = null;

            }
        }
    }

    private void getCashierOpinion (TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
        String cashierOpinion = budgetingProcess.getCashierOpinion();
        cashierOpinion = cashierOpinion.substring(0, cashierOpinion.indexOf("|"));
        String[] split = cashierOpinion.replace("\r\n", "*").split("\\*");

        for (int i = 0, length = split.length; i < length; i++) {
            if (!StringUtils.checkNull(split[i])) {
                String[] split1 = split[i].split("`");
                budgetProcessLog = new BudgetProcessLog();
                setBudgetProcessLog(budgetProcessLog,
                        Integer.valueOf(split1[8]),
                        "7".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?("".equals(split1[7])?"0":"-"+split1[7]):"0",
                        Integer.valueOf(tri.getRunId()),
                        Integer.valueOf(tri.getPrcsId()),
                        tri.getUsers().getUid(),
                        sdf.format(new Date()), "7".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?2:0);
                list.add(budgetProcessLog);
                budgetProcessLog = null;
            }
        }

    }

    // 针对直接结束流程进行 归纳处的减钱日志处理
    private void getCashierOpinion2 (TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list){
        String cashierOpinion = budgetingProcess.getCashierOpinion();
        cashierOpinion = cashierOpinion.substring(0, cashierOpinion.indexOf("|"));
        String[] split = cashierOpinion.replace("\r\n", "*").split("\\*");

        for (int i = 0, length = split.length; i < length; i++) {
            if (!StringUtils.checkNull(split[i])) {
                String[] split1 = split[i].split("`");
                budgetProcessLog = new BudgetProcessLog();
                setBudgetProcessLog(budgetProcessLog,
                        Integer.valueOf(split1[8]),
                        "8".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?("".equals(split1[7])?"0":"-"+split1[7]):"0",
                        Integer.valueOf(tri.getRunId()),
                        Integer.valueOf(tri.getPrcsId()),
                        tri.getUsers().getUid(),
                        sdf.format(new Date()), "8".equals(String.valueOf(Integer.valueOf(tri.getCurrentPrcsId())))?2:0);
                list.add(budgetProcessLog);
                budgetProcessLog = null;
            }
        }

    }

    //出纳步骤日志记录
    private void budgetProcessLogTrue8(TriggerPlugIn tri, BudgetProcessLog budgetProcessLog, BudgetingProcess budgetingProcess, List<BudgetProcessLog> list) {

        if (!StringUtils.checkNull(budgetingProcess.getBreakUpPlan()) && !"``````0``|`````````".equals(budgetingProcess.getBreakUpPlan()) && !"|```````0`".equals(budgetingProcess.getBreakUpPlan()) && !"|`````````".equals(budgetingProcess.getBreakUpPlan())) {
                //出纳处加回会计的本次支出金额
            String breakUpPlan = budgetingProcess.getBreakUpPlan();
            breakUpPlan = breakUpPlan.substring(0, breakUpPlan.indexOf("|"));
            String[] split = breakUpPlan.replace("\r\n", "*").split("\\*");

            for (int i = 0, length = split.length; i < length; i++) {
                if (!StringUtils.checkNull(split[i])) {
                    String[] split1 = split[i].split("`");

                    budgetProcessLog = new BudgetProcessLog();
                    this.setBudgetProcessLog(budgetProcessLog,
                            Integer.valueOf(split1[8]),
                            "+"+split1[7],
                            Integer.valueOf(tri.getRunId()),
                            Integer.valueOf(tri.getPrcsId()),
                            tri.getUsers().getUid(),
                            sdf.format(new Date()), 1);
                    list.add(budgetProcessLog);
                    budgetProcessLog = null;

                }
            }

            if (!StringUtils.checkNull(budgetingProcess.getCashierOpinion()) && !"``````0``|`````````".equals(budgetingProcess.getCashierOpinion())&&!"|```````0`".equals(budgetingProcess.getCashierOpinion())&&!"|`````````".equals(budgetingProcess.getCashierOpinion())) {
                getCashierOpinion2(tri,budgetProcessLog,budgetingProcess,list);
            }
        }else {
            //出纳处加回申请人的本次支出金额
             budgetProcessLog = new BudgetProcessLog();
            setBudgetProcessLog(budgetProcessLog,
                    budgetingProcess.getBudgetId(),
                    "+" + budgetingProcess.getPayment(),
                    Integer.valueOf(tri.getRunId()),
                    Integer.valueOf(tri.getPrcsId()),
                    tri.getUsers().getUid(),
                    sdf.format(new Date()), 1);
            list.add(budgetProcessLog);
            budgetProcessLog = null;
        }

    }

}
